package server.maps;

import java.awt.Point;

import provider.PureData;
import provider.PureDataTool;
import server.maps.PureGenericPortal;
import server.maps.PureMapPortal;

public class PortalFactory {

    private int nextDoorPortal = 0x80;

    public PurePortal makePortal(int type, PureData portal) {
	PureGenericPortal ret = null;
	if (type == PurePortal.MAP_PORTAL) {
	    ret = new PureMapPortal();
	} else {
	    ret = new PureGenericPortal(type);
	}
	loadPortal(ret, portal);
	return ret;
    }

    private void loadPortal(PureGenericPortal myPortal, PureData portal) {
	myPortal.setName(PureDataTool.getString(portal.getChildByPath("pn")));
	myPortal.setTarget(PureDataTool.getString(portal.getChildByPath("tn")));
	myPortal.setTargetMapId(PureDataTool.getInt(portal.getChildByPath("tm")));
	myPortal.setPosition(new Point(PureDataTool.getInt(portal.getChildByPath("x")), PureDataTool.getInt(portal.getChildByPath("y"))));
	String script = PureDataTool.getString("script", portal, null);
	if (script != null && script.equals("")) {
	    script = null;
	}
	myPortal.setScriptName(script);

	if (myPortal.getType() == PurePortal.DOOR_PORTAL) {
	    myPortal.setId(nextDoorPortal);
	    nextDoorPortal++;
	} else {
	    myPortal.setId(Integer.parseInt(portal.getName()));
	}
    }
}
